import copy

import numpy as np
from decode import *


class Individual(object):

    def __init__(self, actStr, modeStr):
        """

        :param actStr: 活动链
        :param modeStr: 模式链
        :param period: 工期
        :param cost: 成本
        """
        self.actStr = actStr
        self.modeStr = modeStr
        self.duration = None
        self.cost = None
        self.resources = None
        self.increasedCost = None

    def fit(self, actList, resources, increasedCost):
        """
        
        :param actList: 
        :param resources: 
        :return: 
        """
        self.resources = resources.copy()
        self.duration = serialGenerationScheme(self.actStr, self.modeStr, actList, resources)
        tmp = 0
        for i in range(0, len(resources)):
            tmp += increasedCost[i] * self.duration  # 在这里乘工期
        self.cost = costProcess(self.actStr, self.modeStr, actList) + tmp

    def __eq__(self, other):
        if (np.array(self.actStr) == np.array(other.actStr)).all() & (
                np.array(self.modeStr) == np.array(other.modeStr)).all():
            return True
        else:
            return False

    def __hash__(self):
        return hash("".join(map(str, self.actStr + self.modeStr)))

    def __str__(self):
        return "duration:{}\t\tcost:{}\tresources:{}\nactStr:{}\nmodeStr:{}\nincreasedCost:{}\n".format(self.duration,
                                                                                                        self.cost,
                                                                                                        self.resources,
                                                                                                        self.actStr,
                                                                                                        self.modeStr,
                                                                                                        self.increasedCost)

    def copy(self):
        indiv = Individual(self.actStr, self.modeStr)
        indiv.resources = copy.deepcopy(self.resources)
        indiv.duration = copy.deepcopy(self.duration)
        indiv.cost = copy.deepcopy(self.cost)
        return indiv
